//
//  NetworkTools+Status.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/21.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit


extension NetworkTools{
    
    func loadStatus(since_id: Int64 = 0, max_id: Int64 = 0, finished:([String: AnyObject]?) -> ()){
        
        guard let accrsstoken = OAuthModel.shareUserAccount.access_token else{
            
            return
        }
        
        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        var params: [String:AnyObject] = ["access_token": accrsstoken]
        
        //Swift中可以将int类型自动转换nsnumber 但是int64不行
        if since_id > 0 {
           params["since_id"] = NSNumber(longLong: since_id)
            
        }
        
        if max_id > 0 {
            params["max_id"] = NSNumber(longLong: max_id-1)
            
        }
        
        request(.GET, URLString: url, parameters: params, finished:finished)
        
        
        
    }
    
/// 发送微博
    func sendStatus(text: String, img: UIImage?,finished:([String: AnyObject]?) -> ()){
        
        guard let accrsstoken = OAuthModel.shareUserAccount.access_token else{
            
            return
        }
        let parpams = ["access_token": accrsstoken,
            "status":text]
        
        if img == nil{
        
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        request(.POST, URLString: url, parameters: parpams, finished: finished)
        
        }else{
            
            let url = "https://upload.api.weibo.com/2/statuses/upload.json"
            
            let data = UIImagePNGRepresentation(img!)
            
            upload(url, parameters: parpams, data: data!, name: "pic", finished: finished)
        
        }
    }
    
    
    
}